package dp.mobile.level;

import java.util.ArrayList;
import org.w3c.dom.Node;

import android.util.Log;
import dp.mobile.system.Utilities;
import dp.mobile.system.XMLReader;

public class CutScene {
	public static class MiniDialog {
		public MiniDialog(Node root) {
			if (root.getAttributes().getNamedItem("speaker") != null)
				mSpeaker = root.getAttributes().getNamedItem("speaker").getNodeValue();
			
			mText = root.getFirstChild().getNodeValue();
		}
		
		public String getSpeaker() {
			return mSpeaker;
		}
		
		public String getText() {
			return mText;
		}
		
		public void setText(String text) {
			mText = text;
		}
		
		private String mSpeaker;
		private String mText;
	}
	
	public static class SubCutScene {
		public SubCutScene(Node root) {
			mCharImage 		= new String[4];
			mCurrentDialog 	= 0;
			mDialogs		= new ArrayList<MiniDialog>();
			int childCount 	= root.getChildNodes().getLength();
			
			for (int i = 0; i < childCount; ++i) {
				Node child = root.getChildNodes().item(i);
				
				if (child.getNodeType() == Node.TEXT_NODE) continue;
				
				String tag = child.getNodeName();
				String val = child.getFirstChild().getNodeValue();
				
				if (tag.equals("char")) {
					if (child.getAttributes().getNamedItem("index") != null)
						mCharImage[Integer.parseInt(child.getAttributes().getNamedItem("index").getNodeValue())] = Utilities.STORY_FOLDER + "/" + val;
				} else if (tag.equals("dialog")) {
					mDialogs.add(new MiniDialog(child));
				}
			}
		}
		
		public String getCharImageResource(int num) {
			return mCharImage[num];
		}
		
		MiniDialog advanceDialog() {
			++mCurrentDialog;
			MiniDialog res = getCurrentDialog();
			
			return res;
		}
		
		MiniDialog getCurrentDialog() {
			if (mCurrentDialog < mDialogs.size())
				return mDialogs.get(mCurrentDialog);
			return null;
		}
		
		private int mCurrentDialog;
		private String[] mCharImage;
		private ArrayList<MiniDialog> mDialogs;
	}
	
	private CutScene() {
		mSubCutScenes 		= new ArrayList<SubCutScene>();
		mBackground			= null;
		mCurrentScene		= 0;
		mSubSceneUpdated	= true;
	}
	
	public CutScene(Node root) {
		this();
		
		int childCount = root.getChildNodes().getLength();
		
		for (int i = 0; i < childCount; ++i) {
			Node child = root.getChildNodes().item(i);
			
			if (child.getNodeType() == Node.TEXT_NODE) continue;
			
			String tag = child.getNodeName();
			String val = child.getFirstChild().getNodeValue();
			
			if (tag.equals("background")) {
				mBackground = Utilities.STORY_FOLDER + "/" + val;
			} else if (tag.equals("sub-cutscene")) {
				mSubCutScenes.add(new SubCutScene(child));
			}
		}
	}
	
	public MiniDialog advanceDialog() {
		mSubSceneUpdated = false;
		if (getCurrentScene().advanceDialog() == null) {
			advanceScene();
		}
		
		if (getCurrentScene() != null)
			return getCurrentScene().getCurrentDialog();
		
		return null;
	}
	
	public SubCutScene advanceScene() {
		mSubSceneUpdated = true;
		++mCurrentScene;
		SubCutScene res = getCurrentScene();
		
		return res;
	}
	
	public MiniDialog getCurrentDialog() {
		return getCurrentScene().getCurrentDialog();
	}
	
	public SubCutScene getCurrentScene() {
		if (mCurrentScene < mSubCutScenes.size())
			return mSubCutScenes.get(mCurrentScene);
		return null;
	}
	
	public String getBackgroundResource() {
		return mBackground;
	}
	
	public boolean isSubSceneUpdated() {
		return mSubSceneUpdated;
	}
	
	public static ArrayList<CutScene> fromLevel(String levelSource) {
		ArrayList<CutScene> retval = new ArrayList<CutScene>();
		
		Node root = XMLReader.instance().parseXML(Utilities.LEVEL_FOLDER + "/" + levelSource);
		String cutscenePath = null;
		int childCount = root.getChildNodes().getLength();
		for (int i = 0; i < childCount; ++i) {
			Node child = root.getChildNodes().item(i);
			
			if (child.getNodeType() == Node.TEXT_NODE) continue;
			
			String tag = child.getNodeName();
			String val = child.getFirstChild().getNodeValue();
			
			if (tag.equals("cutscene")) {
				cutscenePath = val;
				break;
			}
		}
		
		if (cutscenePath == null)
			return null;
		
		Log.d("CutScene", "cutscene path = " + cutscenePath);
		root = XMLReader.instance().parseXML(Utilities.CUTSCENE_FOLDER + "/" + cutscenePath);
		childCount = root.getChildNodes().getLength();
		for (int i = 0; i < childCount; ++i) {
			Node child = root.getChildNodes().item(i);
			
			if (child.getNodeType() == Node.TEXT_NODE) continue;
			
			String tag = child.getNodeName();
			
			if (tag.equals("cutscene")) retval.add(new CutScene(child));
		}
		
		return retval;
	}
	
	private boolean mSubSceneUpdated;
	private int mCurrentScene;
	private ArrayList<SubCutScene> mSubCutScenes;
	private String mBackground;
}
